package br.com.sgpv2.controler.impostoRenda;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import br.com.sgpv2.controler.contribuicaoPrevidenciaria.ContribuicaoPrevidenciariaController;
import br.com.sgpv2.entity.impostorenda.ImpostoRenda;
import br.com.sgpv2.service.impostorenda.ImpostoRendaService;

@Controller
public class ImpostoRendaController {
	
	@Autowired
	private ImpostoRendaService impostoRendaService; 
	
	
	@RequestMapping(value = "impostoRenda/pesquisar", method = RequestMethod.GET)
	public String pesquisar(Model model, HttpServletRequest request) throws Exception {
		String ano = request.getParameter("anoPesquisa");
		String mes = request.getParameter("mesPesquisa");
		
		List<ImpostoRenda> impostos = impostoRendaService.consultarPorMesAno(mes, ano);
		model.addAttribute("impostos", impostos);
		
		model.addAttribute("meses", ContribuicaoPrevidenciariaController.criarListaMeses());
		model.addAttribute("anos", ContribuicaoPrevidenciariaController.criarListaAnos());
		
		return "impostoRenda/impostos";
	}
	
	@RequestMapping(value = "impostoRenda/incluir", method = RequestMethod.GET)
	public String getInclusaoPage(Model model) throws Exception {
		
		model.addAttribute("impostoBean", new ImpostoRenda());
		model.addAttribute("anos", ContribuicaoPrevidenciariaController.criarListaAnos()); //FIXME TVG - Mudar para Util.
		model.addAttribute("meses", ContribuicaoPrevidenciariaController.criarListaMeses()); //FIXME TVG - Mudar para Util.
		model.addAttribute("titleBotaoAcao", "Salvar");
		
		return "impostoRenda/impostoRenda-incluir";
	}
	
	@RequestMapping(value = "impostoRenda/incluirImposto", method = RequestMethod.POST)
	public String salvar(ImpostoRenda impostoRenda, Model model) throws Exception {
		
		impostoRendaService.salvar(impostoRenda);
		return "redirect:pesquisar";
	}
	
	@RequestMapping(value = "impostoRenda/excluir/{pk}", method = RequestMethod.GET)
	public String excluir(@PathVariable Integer pk, Model model) throws Exception {
		ImpostoRenda imposto = impostoRendaService.buscar(pk);
		
		if(imposto !=null){
			impostoRendaService.remover(imposto);
		}
		
		return "redirect:../pesquisar";
	}
	
	@RequestMapping(value = "/impostoRenda/atualizar/{pk}", method = RequestMethod.GET)
	public String getAlteracaoPage(@PathVariable Integer pk,Model model) throws Exception {
		ImpostoRenda imposto = impostoRendaService.buscar(pk);
		
		model.addAttribute("impostoBean", imposto);
		model.addAttribute("anos", ContribuicaoPrevidenciariaController.criarListaAnos());
		model.addAttribute("meses", ContribuicaoPrevidenciariaController.criarListaMeses());
		model.addAttribute("titleBotaoAcao", "Alterar");
		
		return "impostoRenda/impostoRenda-alterar";
	}
	
	@RequestMapping(value = "impostoRenda/alterarImposto", method = RequestMethod.POST)
	public String alterar(ImpostoRenda impostoRenda) throws Exception {
		impostoRendaService.alterar(impostoRenda);		
		
		return "redirect:pesquisar";
	}
}
